//
//  LoginController.m
//  iSwitchView
//
//  Created by Ronnie on 08-11-5.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "LoginController.h"
#import "MainAppController.h"
#import <stdio.h>


@implementation LoginController

- (void) dealloc
{
	[mainWindowCtrl release];
	[mainAppCtrl release];
	[super dealloc];
}

- (void)awakeFromNib
{
	[loginProgress setHidden:YES];
	[cancelButton setEnabled:NO];
	[loginStateButton setMenu:statesMenu];
	
	[[loginStateButton itemAtIndex:1] setTitle:@"available"];
	[[loginStateButton itemAtIndex:1] setImage:[NSImage imageNamed:@"available"]];
	[[loginStateButton itemAtIndex:1] setTarget:self];
	[[loginStateButton itemAtIndex:1] setAction:@selector(onStatusChange:)];
	[[loginStateButton itemAtIndex:1] setState:NSOffState];
	
	
	[[loginStateButton itemAtIndex:3] setTitle:@"busy"];
	[[loginStateButton itemAtIndex:3] setImage:[NSImage imageNamed:@"busy"]];
	[[loginStateButton itemAtIndex:3] setTarget:self];
	[[loginStateButton itemAtIndex:3] setAction:@selector(onStatusChange:)];
	[[loginStateButton itemAtIndex:3] setState:NSOffState];
	
	[[loginStateButton itemAtIndex:4] setTitle:@"meeting"];
	[[loginStateButton itemAtIndex:4] setImage:[NSImage imageNamed:@"busy"]];
	[[loginStateButton itemAtIndex:4] setTarget:self];
	[[loginStateButton itemAtIndex:4] setAction:@selector(onStatusChange:)];
	[[loginStateButton itemAtIndex:4] setState:NSOffState];
	
	[[loginStateButton itemAtIndex:5] setTitle:@"phonecall"];
	[[loginStateButton itemAtIndex:5] setImage:[NSImage imageNamed:@"busy"]];
	[[loginStateButton itemAtIndex:5] setTarget:self];
	[[loginStateButton itemAtIndex:5] setAction:@selector(onStatusChange:)];
	[[loginStateButton itemAtIndex:5] setState:NSOffState];
	
	[[loginStateButton itemAtIndex:7] setTitle:@"away"];
	[[loginStateButton itemAtIndex:7] setImage:[NSImage imageNamed:@"away"]];
	[[loginStateButton itemAtIndex:7] setTarget:self];
	[[loginStateButton itemAtIndex:7] setAction:@selector(onStatusChange:)];
	[[loginStateButton itemAtIndex:7] setState:NSOffState];
	
	[[loginStateButton itemAtIndex:8] setTitle:@"dinner"];
	[[loginStateButton itemAtIndex:8] setImage:[NSImage imageNamed:@"away"]];
	[[loginStateButton itemAtIndex:8] setTarget:self];
	[[loginStateButton itemAtIndex:8] setAction:@selector(onStatusChange:)];
	[[loginStateButton itemAtIndex:8] setState:NSOffState];
	
	[[loginStateButton itemAtIndex:9] setTitle:@"offsite"];
	[[loginStateButton itemAtIndex:9] setImage:[NSImage imageNamed:@"away"]];
	[[loginStateButton itemAtIndex:9] setTarget:self];
	[[loginStateButton itemAtIndex:9] setAction:@selector(onStatusChange:)];
	[[loginStateButton itemAtIndex:9] setState:NSOffState];
	
	[[loginStateButton itemAtIndex:11] setTitle:@"invisible"];
	[[loginStateButton itemAtIndex:11] setImage:[NSImage imageNamed:@"invisible"]];
	[[loginStateButton itemAtIndex:11] setTarget:self];
	[[loginStateButton itemAtIndex:11] setAction:@selector(onStatusChange:)];
	[[loginStateButton itemAtIndex:11] setState:NSOffState];
		
	[[loginStateButton itemAtIndex:1] setState:NSOnState];
	activeItem = [loginStateButton itemAtIndex:1];
	[loginStateButton setTitle:@"available"];
	loginState = FX_STATUS_ONLINE;
	
}
- (IBAction)onStatusChange:(id)sender
{
	NSMenuItem* item = (NSMenuItem*)sender;
	[loginStateButton setTitle:[item title]];
	[activeItem setState:NSOffState];
	[item setState:NSOnState];
	activeItem = item;
	
	NSString* status = [item title];
	if ([status isEqualToString:@"available"])
	{
		loginState = FX_STATUS_ONLINE;
	}
	else if ([status isEqualToString:@"busy"])
	{
		loginState = FX_STATUS_BUSY;
	}
	else if ([status isEqualToString:@"meeting"])
	{
		loginState = FX_STATUS_MEETING;
	}
	else if ([status isEqualToString:@"phonecall"])
	{
		loginState = FX_STATUS_PHONE;
	}
	else if ([status isEqualToString:@"away"])
	{
		loginState = FX_STATUS_AWAY;
	}
	else if ([status isEqualToString:@"dinner"])
	{
		loginState = FX_STATUS_DINNER;
	}
	else if ([status isEqualToString:@"offsite"])
	{
		loginState = FX_STATUS_EXTENDED_AWAY;
	}
	else if ([status isEqualToString:@"invisible"])
	{
		loginState = FX_STATUS_OFFLINE;
	}
	
}
- (IBAction)LoginClick:(id)sender
{
	[loginHint setStringValue:@"logging in"];
	[mainAppCtrl fxLogin:[userName stringValue] password:[passWord stringValue] status:loginState];
	[loginProgress setHidden:NO];
	[cancelButton setEnabled:YES];
	[loginButton setEnabled:NO];
	[loginProgress startAnimation:sender];
	
	
	//[mainWindowCtrl switchToListView];
}

- (IBAction)CancelClick:(id)sender
{
	[mainAppCtrl fxCancelLogin];
	[loginProgress stopAnimation:sender];
	[loginProgress setHidden:YES];
	[cancelButton setEnabled:NO];
	[loginButton setEnabled:YES];
}

- (void)setMyHint:(NSString*)hintMessage
{
	[loginHint setStringValue:hintMessage];
}

- (void)setMainWindowController:(MainWindowController*)p
{
	mainWindowCtrl = [p retain];
}

- (void)setMainAppController:(MainAppController*)p
{
	mainAppCtrl = [p retain];
}

@end
